<?php

declare(strict_types=1);

namespace app\yperp\common;

use app\yperp\model\Supplier;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverKeys;
use think\facade\Cache;

class Changhong extends MyWebDriver
{
    public function __construct()
    {
        $this->redis = Redis::getInstance();
    }
  
    protected function getAuthUrl()
    {

        $url = 'http://mgt.changhong.com/wcm/app/main.jsp';
        $this->driver->get($url);
        sleep(3);
        $currentUrl = $this->driver->getCurrentURL();

        if ($url == $currentUrl) {
            return false;
        }
        // $this->driver->close();
        $url = str_replace('https://cn.changhong.com', "https://chwk1:ch.wk123@cn.changhong.com", $currentUrl);
        $this->driver->get($url);

        return $currentUrl;
    }

    public function run_login()
    {

        $urlAuth = $this->getAuthUrl();
        $this->log('$urlAuth:' . $urlAuth);
        if ($urlAuth) {
            $this->login($urlAuth);
        }
    }

    protected function login($urlAuth)
    {
        // 执行其他操作
        sleep(3);

        $isLogin = false;
        while (!$isLogin) {
            $this->driver->get($urlAuth);

            $user_name_ = 'csmb';
            $user_passwd_ = 'Dragonlhp123';
            $this->log('输入用户名');
            $user_name =  $this->driver->findElement(WebDriverBy::xpath('/html/body/form/div/div/div[2]/dl[2]/dd/div[1]/input'));
            $user_name->clear();
            $user_name->sendKeys($user_name_);
            $this->log('输入密码');

            $user_passwd =  $this->driver->findElement(WebDriverBy::xpath('/html/body/form/div/div/div[2]/dl[2]/dd/div[2]/input'));
            $user_passwd->clear();
            $user_passwd->sendKeys($user_passwd_);

            $this->log('获取验证码图片');
            $imgElement = $this->driver->findElement(WebDriverBy::xpath('/html/body/form/div/div/div[2]/dl[2]/dd/div[3]/span/a/img'));

            $code_img_path = env('root_path')  . 'runtime/mgt_code.png';
            $imgElement->takeElementScreenshot($code_img_path);
            sleep(2);
            $this->log('识别验证码:' . $code_img_path);

            $user_captcha_ = $this->ocrApi($code_img_path);
            // $user_captcha_ = false;
            if (!$user_captcha_) {
                $user_captcha_ = $this->ask('输入验证码：');
            }

            sleep(2);

            $this->log('输入验证码：' . json_encode($user_captcha_));
            $user_captcha =  $this->driver->findElement(WebDriverBy::xpath('/html/body/form/div/div/div[2]/dl[2]/dd/div[3]/input'));
            $user_captcha->clear();
            $user_captcha->sendKeys($user_captcha_);
            $this->log('提交登录');

            $user_captcha->sendKeys(WebDriverKeys::ENTER);
            sleep(3);


            $login_error =  $this->driver->findElements(WebDriverBy::xpath('/html/body/form/div/div/h1/p'));
            if ($login_error) {
                $isLogin = false;
                $this->log('验证码失败，重新登录');
                continue;
            }

            $currentUrl =  $this->driver->getCurrentURL();
            $this->log('登录URL：' . $urlAuth);
            $this->log('当前URL：' . $currentUrl);

            if ($urlAuth == $currentUrl) {
                $isLogin = false;
                $this->log('登录失败，重新登录');
            } else {
                $isLogin = true;
                $this->log('登录成功');
                continue;
            }
            # code...
            sleep(5);
        }
    }
}
